package gr.atc.epart;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;

public class WelcomeActivity extends Activity {

	ProgressBar progressBar;

	private Handler handler; 
	private Activity myActivity;
	private String username;
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
	    myActivity = this;
	    
	    username = AccountUtil.getAccountName(this.getBaseContext());
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.welcomescreen);
		
		progressBar = (ProgressBar) findViewById(R.id.progressbar01);
		
		PackageInfo packageInfo;

		try 
		{
			packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
			String version = "Version " + packageInfo.versionName;
			//+ "Version Code: " + String.valueOf(packageInfo.versionCode);

			TextView versionInfo = (TextView) findViewById(R.id.applicationversion);
			versionInfo.setText(version);
		} catch (NameNotFoundException e) 
		{
			
		}
		handler = new Handler() {
			
			public void handleMessage(Message msg) {
				
				if (progressBar.getProgress() < 99) {
				
					progressBar.incrementProgressBy(33);
					
				} else {
					
					
					Intent ePartDesktopIntent = new Intent();
					ePartDesktopIntent.setClassName("gr.atc.epart",
							"gr.atc.epart.EPartDesktopActivity");
							startActivity(ePartDesktopIntent);
							
							myActivity.finish();
							
				}
			}
		};
		
		fetchingThread.start();

	}

	Thread fetchingThread = new Thread() {

		public void run() {

					handler.sendEmptyMessage(0);
					
					EPartDesktopActivity.retrieveActions(null, null, "all", null, null, "Popularity", "All",
							"Popular");
					
					handler.sendEmptyMessage(0);
					
					EPartDesktopActivity.retrieveActions(null, null, username, null, null, "Popularity",
							"All", "Mine");
					
					handler.sendEmptyMessage(0);
					
					handler.sendEmptyMessage(0);

		}

	};

}